package com.uetoken.cn.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.FeedBackListAdapter;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.FeedBackListBean;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class FeedBackListActivity extends BaseActivity implements WebResponse{

    @BindView(R.id.iv_back)
    ImageView    mIvBack;
    @BindView(R.id.tv_title)
    TextView     mTvTitle;
    @BindView(R.id.ry_activity_feed_back_list)
    RecyclerView mRyFeedBackList;
    @BindView(R.id.rl_activity_feed_back_list_no)
    RelativeLayout mRlNo;

    private FeedBackListAdapter mFeedBackListAdapter;
    private List<FeedBackListBean.DataBean> mDataBeanList = new ArrayList<>();

    private final static int NETWORK_REQUEST_FEED_BACK_LIST = 20;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feed_back_list;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        /*状态栏沉浸式*/
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, android.R.color.white), true);
        BarUtils.setStatusBarLightMode(this, true);

        mTvTitle.setText(getResources().getString(R.string.str_feed_back));
        mIvBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        mRyFeedBackList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        mFeedBackListAdapter = new FeedBackListAdapter(R.layout.item_feed_back_list,mDataBeanList);
        mRyFeedBackList.setAdapter(mFeedBackListAdapter);

        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getFeedBackListUrl(),NETWORK_REQUEST_FEED_BACK_LIST, Params.getFeedBackListParams());

    }

    @Override
    public void init() {

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws IOException {
        dissmissDialog();
        try{
            FeedBackListBean feedBackListBean = new Gson().fromJson(response,FeedBackListBean.class);
            if (feedBackListBean.getResult()>0){
                if (feedBackListBean.getData().size()>0){
                    mRlNo.setVisibility(View.GONE);

                    mDataBeanList.addAll(feedBackListBean.getData());
                    mFeedBackListAdapter.notifyDataSetChanged();
                }else{
                    mRlNo.setVisibility(View.VISIBLE);
                }
            }else{
                ToastUtils.showShort(feedBackListBean.getMessage());
            }
        }catch (JsonParseException e){
            ToastUtils.showShort(getResources().getString(R.string.str_json_parse_exception));
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {
        dissmissDialog();
    }
}
